package com.project.user.server.server;

import com.alibaba.fastjson.JSON;
import com.project.user.entity.entitly.LoginToken;
import com.project.user.entity.entitly.UserInfo;
import com.project.user.entity.entitly.UserInfoExtend;
import com.project.user.entity.entitly.UserInformationView;
import com.project.user.controller.controller.UserCodeController;
import com.project.user.controller.controller.UserInfoController;
import com.project.user.controller.controller.UserInfoExtendController;
import com.project.user.controller.controller.UserTokenController;
import com.untils.Md5Test;
import com.untils.Resultant;
import com.untils.ResultantUtil;
import com.untils.StaticMessage;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class UserServer implements com.project.user.server.UserServer {

    @Override
    public Resultant phonePasswordLogin(String phone, String password,String loginIpv4, String loginIpaddress) throws Exception {
        UserTokenController userTokenController = new UserTokenController();
        UserInfoController userInfoController = new UserInfoController();
        UserInfo userInfo = new UserInfo();
        int successful=-1;
        ResultantUtil resultantUtil=new ResultantUtil();
        String loginSessionId = "";
        Resultant resultant= new Resultant();
        Map<String,String> tokenizes = new HashMap<String,String>();
        UserInfoExtendController userInfoExtendController = new UserInfoExtendController();
        UserInfoExtend userInfoExtend = new UserInfoExtend();
        try {
            userInfo = userInfoController.UserLogin(password,phone);

            loginSessionId=new Md5Test().toMD5(userInfo.getUserId());//加密LXD
            userInfoExtend.setId(userInfo.getId());
            userInfoExtend.setUserEndlogintime(new Date(System.currentTimeMillis()));

            successful=userTokenController.CreateLoginToken(new UserInfo(userInfo.getUserPhonenumber()==null?phone:userInfo.getUserPhonenumber()),loginIpv4,loginIpaddress, userInfo.getUserId()!=null?StaticMessage.CREATE_MESSAGE_SUCCESS:StaticMessage.CREATE_MESSAGE_EXAMPLES,loginSessionId);
            userInfoExtendController.updateUserInfoExample(userInfoExtend);
            tokenizes.put("session",loginSessionId);
            resultant = successful>0 ?resultantUtil.getSuccessResult(StaticMessage.CREATE_PHONE_REQUEST_MESSAGE,JSON.toJSONString(tokenizes)):
                    resultantUtil.getFailResult(StaticMessage.CREATE_PHONE_EXAMPLES_MESSAGE,JSON.toJSONString(tokenizes));
        } catch (Exception e) {
            tokenizes.put("session",StaticMessage.DEFAULT_NULL);
            userTokenController.CreateLoginToken(new UserInfo(phone),loginIpv4,loginIpaddress, StaticMessage.CREATE_MESSAGE_EXAMPLES,loginSessionId);
            resultant=resultantUtil.getFailResult(StaticMessage.CREATE_PHONE_EXAMPLES_MESSAGE,JSON.toJSONString(tokenizes));
        }
        return resultant;
    }

    @Override
    public Resultant phoneCodeLogin(String phone, String phoneCode, String loginIpv4, String loginIpaddress) throws Exception {
        int successful=-1;
        String loginSessionId = "";
        ResultantUtil resultantUtil=new ResultantUtil();
        Resultant resultant= new Resultant();
        Map<String,String> tokenizes = new HashMap<String,String>();
        UserTokenController userTokenController = new UserTokenController();
        UserCodeController userCodeController = new UserCodeController();
        UserInfoExtendController userInfoExtendController = new UserInfoExtendController();
        UserInfoExtend userInfoExtend = new UserInfoExtend();
        try {
            Map<String, Object> successfully=userCodeController.getUserPhoneCode(phone,phoneCode);
            UserInformationView userInfo = (UserInformationView) successfully.get("is-object");
            loginSessionId=new Md5Test().toMD5(userInfo.getUserId());//加密LXD
            userInfoExtend.setId(userInfo.getId());
            userInfoExtend.setUserEndlogintime(new Date(System.currentTimeMillis()));
            boolean isSuccessfully= Boolean.parseBoolean(String.valueOf(successfully.get("is-successfully")));
            successful=userTokenController.CreateLoginToken(new UserInfo(userInfo.getUserPhonenumber()==null?phone:userInfo.getUserPhonenumber()),loginIpv4,loginIpaddress, isSuccessfully?StaticMessage.CREATE_MESSAGE_SUCCESS:StaticMessage.CREATE_MESSAGE_EXAMPLES,loginSessionId);
            tokenizes.put("session",loginSessionId);
            userInfoExtendController.updateUserInfoExample(userInfoExtend);
            resultant = isSuccessfully && successful>0 ?
                    resultantUtil.getSuccessResult(StaticMessage.CREATE_PHONE_REQUEST_MESSAGE,JSON.toJSONString(tokenizes)):
                    resultantUtil.getFailResult(StaticMessage.CREATE_PHONE_EXAMPLES_MESSAGE,JSON.toJSONString(tokenizes));
        } catch (Exception e) {
            e.printStackTrace();
            tokenizes.put("session",StaticMessage.DEFAULT_NULL);
            userTokenController.CreateLoginToken(new UserInfo(phone),loginIpv4,loginIpaddress, StaticMessage.CREATE_MESSAGE_EXAMPLES,loginSessionId);
            resultant=resultantUtil.getFailResult(StaticMessage.CREATE_PHONE_EXAMPLES_MESSAGE,JSON.toJSONString(tokenizes));
        }
        return resultant;
    }

    @Override
    public Resultant setUserPhoneCode(String phone) {
        UserCodeController userCodeController = new UserCodeController();
        ResultantUtil resultantUtil=new ResultantUtil();
        Resultant resultant= new Resultant();
        try {
            int successfully=userCodeController.setUserPhoneCode(phone);
            resultant=successfully>0?resultantUtil.getSuccessResult(StaticMessage.CREATE_PHONE_REQUEST_MESSAGE,null):
                    resultantUtil.getFailResult(StaticMessage.CREATE_PHONE_EXAMPLES_MESSAGE, null);
        } catch (Exception e) {
            resultant=resultantUtil.getFailResult(StaticMessage.CREATE_PHONE_EXAMPLES_MESSAGE,null);
        }
        return resultant;
    }

    @Override
    public Resultant getUInformation(String session) {
        ResultantUtil resultantUtil=new ResultantUtil();
        Resultant resultant= new Resultant();
        try {
            resultant=resultantUtil.getSuccessResult(StaticMessage.GET_USER_SESSION_SUCCESS_MESSAGE,JSON.toJSONString(this.getUInfo(session)));
        } catch (Exception e) {
            resultant=resultantUtil.getFailResult(StaticMessage.GET_USER_SESSION_EXAMPLE_MESSAGE,null);
        }
        return resultant;
    }

    @Override
    public Resultant signOut(String session) {
        UserTokenController userTokenController = new UserTokenController();
        ResultantUtil resultantUtil=new ResultantUtil();
        Resultant resultant= new Resultant();
        try {
            LoginToken loginToken=userTokenController.CheckLoginToken(null,null,session);
            loginToken.setLoginSessionId(StaticMessage.DEFAULT_NULL);
            userTokenController.updateLoginToken(loginToken);
        } catch (Exception e) {

            resultant=resultantUtil.getFailResult(StaticMessage.GET_USER_SESSION_EXAMPLE_MESSAGE,null);
        }
        return resultant;
    }

    @Override
    public UserInformationView getUInfo(String session) throws Exception{
        UserTokenController userTokenController = new UserTokenController();
        UserInfoController userInfoController = new UserInfoController();
        UserInformationView userInformationView = new UserInformationView();
        LoginToken loginToken=userTokenController.CheckLoginToken(null,null,session);
        userInformationView=userInfoController.UserInfo(-1,loginToken.getLoginUserid(),null);
        return userInformationView;
    }

    @Override
    public Resultant updateUserInfo(String session, UserInfo user) {
        UserInfoController userInfoController = new UserInfoController();
        ResultantUtil resultantUtil=new ResultantUtil();
        Resultant resultant= new Resultant();
        Map<String,String> tokenizes = new HashMap<String,String>();
        try {
            if (this.getUInfo(session).getUserId().equals(user.getUserId())) {
                int successfully = userInfoController.updateUserInfo(user);
                resultant = successfully>0?
                        resultantUtil.getSuccessResult(StaticMessage.CREATE_PHONE_REQUEST_MESSAGE,null):
                        resultantUtil.getFailResult(StaticMessage.CREATE_PHONE_EXAMPLES_MESSAGE,null);
            }else{
                resultant=resultantUtil.getFailResult(StaticMessage.GET_USER_SESSION_EXAMPLE_MESSAGE,null);
            }
        } catch (Exception e) {
            resultant=resultantUtil.getFailResult(StaticMessage.GET_USER_SESSION_EXAMPLE_MESSAGE,null);
        }
        return resultant;
    }

    @Override
    public Resultant instepUserInfo(String phone, String password) {
        UserInfoController userInfoController = new UserInfoController();
        ResultantUtil resultantUtil=new ResultantUtil();
        Resultant resultant= new Resultant();
        try {
            Map<String, Object> map=userInfoController.UserInsert(password,phone);
            resultant = Boolean.parseBoolean(String.valueOf(map.get("is-successfully")))?
                    resultantUtil.getSuccessResult(StaticMessage.CREATE_MESSAGE,null):
                    resultantUtil.getFailResult(StaticMessage.CREATE_PHONE_EXAMPLES_MESSAGE,null);
        } catch (Exception e) {
            resultant=resultantUtil.getFailResult(StaticMessage.PHONE_REQUEST_MESSAGE,null);
        }
        return resultant;
    }

    @Override
    public boolean userIsLoginEd(String phone, String userIpV4) {
        UserTokenController userTokenController = new UserTokenController();
        UserInfo user = new UserInfo();
        user.setUserId(phone);
        LoginToken loginToken=new LoginToken();
        boolean isValid = false;
        try {
            loginToken=this.userTokenGet(phone);
            isValid=loginToken.getLoginUserid().length()>0 && loginToken.getLoginIpv4().equals(userIpV4);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return isValid;
    }

    @Override
    public LoginToken userTokenGet(String phone) throws Exception {
        UserTokenController userTokenController = new UserTokenController();
        UserInfo user = new UserInfo();
        user.setUserId(phone);
        LoginToken loginToken=new LoginToken();
        loginToken=userTokenController.CheckLoginToken(user,null,null);
        return loginToken;
    }
}
